package com.mef.smsplat.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.BaseJdbcDao;
import com.mef.smsplat.dao.base.BaseSpringJdbcDao;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.RoleInfoDomain;
import com.mef.smsplat.util.Constants;

@Repository
public class RoleInfoDao extends BaseSpringJdbcDao<RoleInfoDomain, String> {
	
	/**
	 * 根据角色名分页查询角色信息
	 * @param roleName
	 * @param dgm
	 * @return
	 */
	public Page query(String roleName,DataGridModel dgm){
		List<Object> params=new ArrayList<Object>();
		StringBuilder sql=new StringBuilder();
		sql.append("select * from role_info where 1=1");
		if(roleName!=null&&!"".equals(roleName)){
			sql.append(" and role_name like ?");
			params.add("%"+roleName+"%");
		}
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params);
	}
	
	/**
	 * 根据角色名和id查询角色是否唯一
	 * @param name
	 * @param id
	 * @return
	 */
	public boolean queryByNameAndId(String name,String id){
		String sql="select * from role_info t where t.role_name=? and t.role_id!=?";
		List<RoleInfoDomain> list=super.query(sql, new Object[]{name,id});
		if(list.size()>0)
			return false;
		return true;
	}
	
	/**
	 * 查询所有角色信息
	 * @return
	 */
	public List<RoleInfoDomain> queryAll(){
		StringBuilder sql=new StringBuilder();
		sql.append("select * from role_info where status=?");
		return super.query(sql.toString(),new Object[]{Constants.NORMAL});
	}
	
	/**
	 * 根据roleId查询role
	 * @param roleId
	 * @return
	 */
	public RoleInfoDomain queryByRoleId(String roleId){
		String sql="select * from role_info t where t.role_id=?";
		List<RoleInfoDomain> list=super.query(sql, new Object[]{roleId});
		if(list.size()>0)
			return list.get(0);
		return null;
	}

	@Override
	public BaseJdbcDao getDaoClass() {
		return this;
	}

	@Override
	public Class getEntityClass() {
		return RoleInfoDomain.class;
	}
}
